package cn.tqfeiyang.smartadmin.commons.security;

/**
 * 安全上下文
 *
 * @author tqfeiyang
 * @since 2024/8/22 8:53
 */
public class SecurityContext {

    private static final ThreadLocal<UserDetails> REQUEST_THREAD_LOCAL = new ThreadLocal<>();

    public static void setCurrentUser(UserDetails requestUser) {
        REQUEST_THREAD_LOCAL.set(requestUser);
    }

    public static UserDetails getCurrentUser() {
        return REQUEST_THREAD_LOCAL.get();
    }

    public static void cleanCurrentUser() {
        REQUEST_THREAD_LOCAL.remove();
    }

    public static Long getUserId() {
        UserDetails requestUser = getCurrentUser();
        return null == requestUser ? null : requestUser.getUserId();
    }

    public static UserDetails getUserDetails() {
        return getCurrentUser();
    }

}
